package com.example.memosystem.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.memosystem.R;
import com.example.memosystem.dao.impl.AdminDao;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

public class AddActivity extends AppCompatActivity {

    String uuid = null;
    String account = null;
    EditText res = null;
    EditText title = null;
    TextView textView = null;
    @SuppressLint("SimpleDateFormat")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_add);

        //加载菜单
        Toolbar toolbar = findViewById(R.id.toolbar_add);
        this.setSupportActionBar(toolbar);

        //获取账号
        Intent intent = getIntent();
        account = intent.getStringExtra("account");


        textView = findViewById(R.id.add_time);
        Calendar calendar = Calendar.getInstance();
         SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
        String curTime = dateFormat.format(calendar.getTime());
        curTime = curTime + "    |     ";
        curTime=curTime+"字";
        textView.setText(curTime);

        //确定时间根据修改的时间来定
        res = findViewById(R.id.add_put);
        title = findViewById(R.id.add_title);

        //判断是添加界面还是更改界面
        account = intent.getStringExtra("account");
        String s = intent.getStringExtra("sta");
        if(s == null)
        {
            uuid = UUID.randomUUID().toString();
        }else {
            intent.putExtra("sta", "1");
            uuid = intent.getStringExtra("id");
            res.setText(intent.getStringExtra("con"));
            title.setText(intent.getStringExtra("title"));
            textView.setText(intent.getStringExtra("time"));
        }

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(AddActivity .this, ManageActivity.class);
                intent1.putExtra("account", account);
                startActivity(intent1);
            }
        });

        res.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //获取当前时间日期
                Calendar calendar = Calendar.getInstance();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 a HH:mm");
                String curTime = dateFormat.format(calendar.getTime());

                curTime = curTime + "    |     ";

                String text = String.valueOf(res.getText());
                String zi = String.valueOf(text.length());
                curTime=curTime+zi+"字";
                textView.setText(curTime);

                //没输入一个字符都自动保存
                String titles = title.getText().toString().trim();
                String rest = res.getText().toString().trim();
                int a = new AdminDao().saveRecord(uuid, account, titles, rest, curTime);

            }
            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //构造菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.add_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemID = item.getItemId();
        if(itemID == R.id.menu_add_save)//保存
        {
            String curTime = textView.getText().toString();
            String titles = title.getText().toString().trim();
            String rest = res.getText().toString().trim();
            int a = new AdminDao().saveRecord(uuid, account, titles, rest, curTime);
            Intent intent = new Intent(AddActivity.this, ManageActivity.class);
            intent.putExtra("account", account);
            startActivity(intent);

        }else if(itemID == R.id.menu_add_del)
        {
            int a = new AdminDao().delRecord(uuid, account);
            Intent intent = new Intent(AddActivity.this, ManageActivity.class);
            intent.putExtra("account", account);
            startActivity(intent);
        }
        return super.onOptionsItemSelected(item);
    }
}